package com.yuqing.sentimentapp.mine;

import android.app.Dialog;
import android.app.Notification;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linkface.baselibrary.http.BaseBean;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseFragment;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.login.acitivity.LoginActivity;
import com.yuqing.sentimentapp.mine.bean.MinePersonInfoBean;
import com.yuqing.sentimentapp.mine.presenter.MinePersonInfoPresenter;
import com.yuqing.sentimentapp.mine.presenter.impl.MinePersonInfoPresenterImpl;
import com.yuqing.sentimentapp.mine.view.MineView;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.utils.GlideUtil;
import com.yuqing.sentimentapp.utils.SharePrefrenceHelper;
import com.yuqing.sentimentapp.utils.ToastUtil;
import com.yuqing.sentimentapp.widget.dialog.TipsTwoBtnDialog;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment implements MineView<MinePersonInfoBean> {

    @BindView(R.id.tv_logout)
    TextView mTvLogout;
    @BindView(R.id.iv_head)
    ImageView mIvHead;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_nesc)
    TextView mTvNesc;
    @BindView(R.id.cb_wechat)
    CheckBox mCbWechat;
    @BindView(R.id.cb_app_client)
    CheckBox mCbAppClient;
    @BindView(R.id.cb_voice)
    CheckBox mCbVoice;
    @BindView(R.id.cb_shock)
    CheckBox mCbShock;
    @BindView(R.id.layout_yuqing_about)
    RelativeLayout mLayoutYuqingAbout;
    private MinePersonInfoPresenter mPresenter;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mPresenter.loadPersonCenterInfo(SharePrefrenceHelper.getLoginMobile());
    }

    @Override
    protected void initPresenter() {
        mPresenter = new MinePersonInfoPresenterImpl(this);
    }

    @OnClick({R.id.tv_logout, R.id.layout_yuqing_about, R.id.cb_voice, R.id.cb_shock, R.id.cb_wechat, R.id.cb_app_client})
    public void onClick(View view) {
        switch (view.getId()) {
            default:
                break;
            case R.id.cb_wechat:
                mCbWechat.setChecked(mCbWechat.isChecked());
                setSoundAndVibrate();
                break;
            case R.id.cb_app_client:
                mCbAppClient.setChecked(mCbAppClient.isChecked());
                setSoundAndVibrate();
                break;
            case R.id.cb_voice:
                mCbVoice.setChecked(mCbVoice.isChecked());
                setSoundAndVibrate();
                break;
            case R.id.cb_shock:
                mCbShock.setChecked(mCbShock.isChecked());
                setSoundAndVibrate();
                break;
            case R.id.tv_logout:
                TipsTwoBtnDialog dialog = new TipsTwoBtnDialog(getActivity());
                dialog.setContent("确定退出登录吗？");
                dialog.setTitle("退出登录");
                dialog.setButtonListener(new TipsTwoBtnDialog.ButtonListener() {
                    @Override
                    public void buttonOkClick(Dialog dialog) {
                        logout();
                        SharePrefrenceHelper.clearUserData();
                        //关闭极光推送
                        if (!JPushInterface.isPushStopped(getActivity().getApplicationContext())) {
                            JPushInterface.stopPush(getActivity().getApplicationContext());
                        }
                        turnToLogin();
                    }

                    @Override
                    public void buttonCancleClick(Dialog dialog) {
                    }
                });
                dialog.show();
                break;
            case R.id.layout_yuqing_about:
                Intent intent = new Intent(getActivity(), YqInfoAboutActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void logout() {
        ModuleManager.getInstance().postLogoutRequest(getActivity())
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new BaseObserver<BaseBean>() {
            @Override
            protected void hideDialog() {
                stopProgressDialog();
            }

            @Override
            protected void showDialog() {
                startProgressDialog();
            }

            @Override
            protected void onSuccess(BaseBean baseBean) {

            }

            @Override
            protected void onFailure(String message) {

            }
        });
    }

    private void turnToLogin() {
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
        getActivity().finish();
    }

    @Override
    public void onSuccess(MinePersonInfoBean data) {
        MinePersonInfoBean.DataBean minePersonInfo = data.getData();
        if (null == minePersonInfo) {
            mTvName.setText("请登录");
            return;
        }
        mTvName.setText(minePersonInfo.getUsername());
        GlideUtil.loadCirclePic(getActivity(), minePersonInfo.getHeadImg(), mIvHead);
        mCbAppClient.setChecked(minePersonInfo.getAppNotice() == 1);
        mCbWechat.setChecked(minePersonInfo.getWxNotice() == 1);
        int buildId = minePersonInfo.getBuildId();
        switch (buildId) {
            default:
                mCbShock.setChecked(true);
                mCbVoice.setChecked(true);
                break;
            case 1:
                mCbShock.setChecked(false);
                mCbVoice.setChecked(true);
                break;
            case 2:
                mCbShock.setChecked(true);
                mCbVoice.setChecked(false);
                break;
            case -1:
                mCbShock.setChecked(true);
                mCbVoice.setChecked(true);
                break;
            case 4:
                mCbShock.setChecked(false);
                mCbVoice.setChecked(false);
                break;
        }
    }

    @Override
    public void onFailure(String message) {
        ToastUtil.showShort(message);
        mTvName.setText("请登录");
    }

    private void setSoundAndVibrate(){
        BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(getActivity());
        int builder_id = Notification.DEFAULT_ALL;
        builder.statusBarDrawable = R.drawable.ic_logo;//设置推送的图标
        if (!mCbVoice.isChecked() && mCbShock.isChecked()) {//只有振动 build_id = 2
            builder.notificationDefaults = Notification.DEFAULT_VIBRATE;
            builder_id = Notification.DEFAULT_VIBRATE;
        }
        if (mCbVoice.isChecked() && !mCbShock.isChecked()) {//只有声音 build_id = 1
            builder.notificationDefaults = Notification.DEFAULT_SOUND;
            builder_id = Notification.DEFAULT_SOUND;
        }
        if (mCbVoice.isChecked() && mCbShock.isChecked()) {//两个都有 build_id = -1
            builder.notificationDefaults = Notification.DEFAULT_ALL;
            builder_id = Notification.DEFAULT_ALL;
        }
        if (!mCbVoice.isChecked() && !mCbShock.isChecked()){//只有呼吸灯 build_id = 4
            builder.notificationDefaults = Notification.DEFAULT_LIGHTS;
            builder_id = Notification.DEFAULT_LIGHTS;
        }
        JPushInterface.setPushNotificationBuilder(builder_id, builder);
        postNoticeMessage(builder_id);
    }

    private void postNoticeMessage(int builder_id) {
        ModuleManager.getInstance().postNoticeResult(getActivity(), mCbWechat.isChecked() ? 1 : 0, mCbAppClient.isChecked() ? 1 : 0, builder_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {

                    }

                    @Override
                    protected void onFailure(String message) {

                    }
                });
    }
}
